package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.crm.entity.*;
import com.crm.mapper.*;
import com.crm.service.ICrmCustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.util.DateUtils;
import com.crm.util.UUIDUtils;
import com.crm.vo.Clue;
import com.crm.vo.ClueActivityRelation;
import com.crm.vo.ClueRemark;
import com.crm.vo.ConvertClueVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author junxian,dou
 * @since 2024-11-25
 */
@Service
public class CrmCustomerServiceImpl extends ServiceImpl<CrmCustomerMapper, CrmCustomer> implements ICrmCustomerService {

    @Resource
    private CrmClueMapper crmClueMapper;

    @Resource
    private CrmContactsMapper crmContactsMapper;

    @Resource
    private CrmClueActivityRelationMapper clueActivityRelationMapper;

    @Resource
    private CrmClueRemarkMapper crmClueRemarkMapper;

    @Resource
    private CrmCustomerRemarkMapper customerRemarkMapper;

    @Resource
    private CrmContactsRemarkMapper contactsRemarkMapper;

    @Resource
    private CrmContactsActivityRelationMapper contactsActivityRelationMapper;

    @Resource
    private CrnTranMapper crnTranMapper;

    @Resource
    private CrmTranRemarkMapper tranRemarkMapper;

    @Transactional
    @Override
    public void convertClue(ConvertClueVo convertClue, CrmUser user) {
        String clueId=convertClue.getClueId();
        //根据id查询线索的信息
        CrmClue clue = crmClueMapper.selectById(clueId);
        //把该线索中有关公司的信息转换到客户表中
        CrmCustomer c=new CrmCustomer();
        c.setAddress(clue.getAddress());
        c.setContactSummary(clue.getContactSummary());
        c.setDescription(clue.getDescription());
        c.setId(UUIDUtils.getUUID());
        c.setName(clue.getCompany());
        c.setNextContactTime(clue.getNextContactTime());
        c.setOwner(user.getId());
        c.setPhone(clue.getPhone());
        c.setWebsite(clue.getWebsite());
        save(c);
        //把该线索中有关个人的信息转换到联系人表中
        CrmContacts co=new CrmContacts();
        co.setAddress(clue.getAddress());
        co.setAppellation(clue.getAppellation());
        co.setContactSummary(clue.getContactSummary());
        co.setCustomerId(c.getId());
        co.setDescription(clue.getDescription());
        co.setEmail(clue.getEmail());
        co.setFullname(clue.getFullname());
        co.setId(UUIDUtils.getUUID());
        co.setJob(clue.getJob());
        co.setMphone(clue.getMphone());
        co.setNextContactTime(clue.getNextContactTime());
        co.setOwner(user.getId());
        co.setSource(clue.getSource());
        crmContactsMapper.insert(co);

        //根据clueId查询该线索下所有的备注
        LambdaQueryWrapper<CrmClueRemark> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CrmClueRemark::getClueId,clueId);
        List<CrmClueRemark> crList= crmClueRemarkMapper.selectList(wrapper);

        //如果该线索下有备注，把该线索下所有的备注转换到客户备注表中一份,把该线索下所有的备注转换到联系人备注表中一份
        if(crList!=null&&crList.size()>0){
            //遍历crList，封装客户备注
            CrmCustomerRemark cur=null;
            CrmContactsRemark cor=null;
            List<CrmCustomerRemark> curList=new ArrayList<>();
            List<CrmContactsRemark> corList=new ArrayList<>();
            for(CrmClueRemark cr:crList){
                cur=new CrmCustomerRemark();
                cur.setCustomerId(c.getId());
                cur.setEditFlag(cr.getEditFlag());
                cur.setId(UUIDUtils.getUUID());
                cur.setNoteContent(cr.getNoteContent());
                curList.add(cur);

                cor=new CrmContactsRemark();
                cor.setContactsId(co.getId());
                cor.setEditFlag(cr.getEditFlag());
                cor.setId(UUIDUtils.getUUID());
                cor.setNoteContent(cr.getNoteContent());
                corList.add(cor);
            }
            customerRemarkMapper.insertCustomerRemarkByList(curList);
            contactsRemarkMapper.insertContactsRemarkByList(corList);
        }

        //根据clueId查询该线索和市场活动的关联关系
        LambdaQueryWrapper<CrmClueActivityRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CrmClueActivityRelation::getClueId,clueId);
        List<CrmClueActivityRelation> carList=clueActivityRelationMapper.selectList(queryWrapper);

        //把该线索和市场活动的关联关系转换到联系人和市场活动的关联关系表中
        if(carList!=null&&carList.size()>0){
            CrmContactsActivityRelation coar=null;
            List<CrmContactsActivityRelation> coarList=new ArrayList<>();
            for(CrmClueActivityRelation car:carList){
                coar=new CrmContactsActivityRelation();
                coar.setActivityId(car.getActivityId());
                coar.setContactsId(co.getId());
                coar.setId(UUIDUtils.getUUID());
                coarList.add(coar);
            }
            contactsActivityRelationMapper.insertContactsActivityRelationByList(coarList);
        }

        String isCreateTran=convertClue.getIsCreateTran();
        //如果需要创建交易，则往交易表中添加一条记录,还需要把该线索下的备注转换到交易备注表中一份
        if("true".equals(isCreateTran)){
            CrnTran tran=new CrnTran();
            tran.setActivityId(convertClue.getActivityId());
            tran.setContactsId(co.getId());
            tran.setCustomerId(c.getId());
            tran.setExpectedDate(convertClue.getExpectedDate());
            tran.setId(UUIDUtils.getUUID());
            tran.setMoney(convertClue.getMoney());
            tran.setName(convertClue.getName());
            tran.setOwner(user.getId());
            tran.setStage(convertClue.getStage());
            crnTranMapper.insert(tran);

            if(crList!=null&&crList.size()>0){
                CrmTranRemark tr=null;
                List<CrmTranRemark> trList=new ArrayList<>();
                for(CrmClueRemark cr:crList){
                    tr=new CrmTranRemark();
                    tr.setEditFlag(cr.getEditFlag());
                    tr.setId(UUIDUtils.getUUID());
                    tr.setNoteContent(cr.getNoteContent());
                    tr.setTranId(tran.getId());
                    trList.add(tr);
                }

                tranRemarkMapper.insertTranRemarkByList(trList);
            }
        }

        //删除该线索下所有的备注
        crmClueRemarkMapper.deleteClueRemarkByClueId(clueId);

        //删除该线索和市场活动的关联关系
        clueActivityRelationMapper.deleteClueActivityRelationByClueId(clueId);

        //删除线索
        crmClueMapper.deleteClueById(clueId);

    }
}
